import pygame
import math
from animation import Animation
from level_resources import LevelResources
from settings_manager import SettingsManager

tiles = 15

class Bomb:
	def __init__(self, screen, group, power, pNr):
		self.width, self.height = SettingsManager.settings.resolution[0] / tiles, SettingsManager.settings.resolution[0] / tiles

		self.screen = screen
		self.group = group
		self.rect = pygame.Rect(0, 0, self.width, self.height)

		self.anim = Animation(screen, self.rect,
			LevelResources.bImages, 
			LevelResources.bDelays, True, 2000)

		self.planted = False
		self.exploded = False
		self.power = power
		self.pNr = pNr
		self.points = 0

	def setLocation(self, x, y):
		self.rect.x, self.rect.y = x, y
		self.anim.rect = self.rect

	@staticmethod
	def setPlayers(players):
		Bomb.players = players

	@staticmethod
	def setObstacles(obstacles):
		Bomb.obstacles = obstacles

	def plant(self, x, y):
		self.setLocation(x, y)

		self.planted = True
		self.group.add(self.anim)
		self.anim.startAnimation()
		self.lastTime = pygame.time.get_ticks()

	def explode(self):
		self.anim.kill()
		self.exploded = True

		self.anim = Animation(self.screen, self.rect,
			LevelResources.bExpImages,
			LevelResources.bExpDelays, False, 0)
		
		self.group.add(self.anim)
		self.anim.startAnimation()
		
		i, j = int((self.rect.x + self.width / 2) / self.width), int((self.rect.y + self.height / 2) / self.height)

		pij = []
		for player in Bomb.players:
			pij.append(player.getPosCentered())

		#Center
		if (i, j) in Bomb.obstacles and not Bomb.obstacles[(i, j)].destroyed:
				Bomb.obstacles[(i, j)].hit()
				self.points += Bomb.obstacles[(i, j)].obType * 100 + 50
		for player in Bomb.players:
			if player.getPosCentered() == (i, j):
				player.hit(int(player.pNr != self.pNr) + 1)
				if player.pNr != self.pNr:
					self.points += 1000
				else:
					self.points -= 500

		#Left
		t = True
		for ii in xrange(1, self.power):
			if  i - ii < 0 or not t:
				break
			if (i - ii, j) in Bomb.obstacles and not Bomb.obstacles[(i - ii, j)].destroyed:
				Bomb.obstacles[(i - ii, j)].hit()
				self.points += Bomb.obstacles[(i - ii, j)].obType * 100 + 50
				t = False
			for player in Bomb.players:
				if player.getPosCentered() == (i - ii, j):
					player.hit(int(player.pNr != self.pNr) + 1)
					if player.pNr != self.pNr:
						self.points += 1000
					else:
						self.points -= 500

		#Right
		t = True
		for ii in xrange(1, self.power):
			if  i + ii >= tiles or not t:
				break
			if (i + ii, j) in Bomb.obstacles and not Bomb.obstacles[(i + ii, j)].destroyed:
				Bomb.obstacles[(i + ii, j)].hit()
				self.points += Bomb.obstacles[(i + ii, j)].obType * 100 + 50
				t = False
			for player in Bomb.players:
				if player.getPosCentered() == (i + ii, j):
					player.hit(int(player.pNr != self.pNr) + 1)
					if player.pNr != self.pNr:
						self.points += 1000
					else:
						self.points -= 500
				
		#Up
		t = True
		for jj in xrange(1, self.power):
			if  j - jj < 0 or not t:
				break
			if (i, j - jj) in Bomb.obstacles and not Bomb.obstacles[(i, j - jj)].destroyed:
				Bomb.obstacles[(i, j - jj)].hit()
				self.points += Bomb.obstacles[(i, j - jj)].obType * 100 + 50
				t = False
			for player in Bomb.players:
				if player.getPosCentered() == (i, j - jj):
					player.hit(int(player.pNr != self.pNr) + 1)
					if player.pNr != self.pNr:
						self.points += 1000
					else:
						self.points -= 500

		#Down
		t = True
		for jj in xrange(1, self.power):
			if  j + jj >= tiles or not t:
				break
			if (i, j + jj) in Bomb.obstacles and not Bomb.obstacles[(i, j + jj)].destroyed:
				Bomb.obstacles[(i, j + jj)].hit()
				self.points += Bomb.obstacles[(i, j + jj)].obType * 100 + 50
				t = False
			for player in Bomb.players:
				if player.getPosCentered() == (i, j + jj):
					player.hit(int(player.pNr != self.pNr) + 1)
					if player.pNr != self.pNr:
						self.points += 1000
					else:
						self.points -= 500

	def update(self):
		if self.exploded:
			return None

		currentTime = pygame.time.get_ticks()
		if self.planted:
			if currentTime - self.lastTime > 2000:	#Planted 2 secs ago
				self.explode()